package march.view 
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.ui.Keyboard;
	import interfaces.IOperate;
	import march.MarchC;
	import march.model.PlotVO;
	import morn.core.components.Clip;
	import org.airmvc.Dispatcher;
	/**
	 * ...
	 * @author WLDragon 2014-04-30
	 */
	public class Plot extends Sprite implements IOperate
	{
		
		public function Plot() 
		{
			var bg:Bitmap = new Bitmap(App.asset.getBitmapData("png.march.framework"));
			bg.x = 18;
			bg.y = 3;
			addChild(bg);
			
			arrow = new Clip("png.common.clip_arrow", 1, 2);
			arrow.mouseChildren = false;
			arrow.mouseEnabled = false;
			arrow.frame = 1;
			arrow.y = -16;
			addChild(arrow);
			
			itemPanel = new Sprite();
			itemPanel.x = 64;
			itemPanel.y = 36;
			addChild(itemPanel);
			
			itemList = [];
		}
		
		public function execute(keyCode:int):void
		{
			switch (keyCode) 
			{
				case Keyboard.A:
					index--;
				break;
				case Keyboard.D:
					index++;
				break;
				case Keyboard.W:
					index -= 2;
				break;
				case Keyboard.S:
					index += 2;
				break;
				case Keyboard.R:
					Dispatcher.send(MarchC.CLOSE_PLOT,this);
				break;
				case Keyboard.SPACE:
					if(step >= curItem.data.step)
						Dispatcher.send(MarchC.USE_PLOT, this, curItem.data);
					else
						Msg.instance.show("机动力不足，请选择其他计谋");
				break;
				
			}
		}
		
		public function update(data:Array,step:int = 0):void
		{
			if (data)
			{
				this.step = step;
				while (itemPanel.numChildren > 0)
				{
					itemPanel.removeChildAt(0);
				}
				
				itemList.length = 0;
				for (var i:int = 0, len:int = data.length; i < len; i++) 
				{
					var item:PlotItem = new PlotItem(data[i]);
					item.x = 260 * (i % 2);
					item.y = 40 * (i/2>>0);
					itemList.push(item);
					itemPanel.addChild(item);
				}
				
				index = 0;
			}
			Kyb.setOperater(this);
		}
		
		public function get index():int 
		{
			return _index;
		}
		
		public function set index(value:int):void 
		{
			if (value < 0 || value >= itemList.length)
				return;
				
			curItem = itemList[value];
			arrow.x = curItem.x + 64 - 16;
			arrow.y = curItem.y + 40;
			_index = value;
		}
		
		private var itemPanel:Sprite;
		private var arrow:Clip;
		private var itemList:Array;
		private var curItem:PlotItem;
		private var _index:int;
		private var step:int;
	}

}